package cn.adelyn.blog.file.controller;

import cn.adelyn.base.secureweb.AuthUserContext;
import cn.adelyn.blog.file.constant.MinioConstant;
import cn.adelyn.blog.file.pojo.dto.InsertFileInfoDTO;
import cn.adelyn.blog.file.pojo.dto.UpdateFileInfoDTO;
import cn.adelyn.blog.file.pojo.vo.FileUrlVO;
import cn.adelyn.blog.file.service.FileService;
import cn.adelyn.common.core.response.ServerResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;

@Validated
@RestController
@RequestMapping("/file")
public class FileController {

    @Autowired
    FileService fileService;

    @PostMapping("/info")
    public ServerResponseEntity insertFileInfo(@RequestBody @Valid InsertFileInfoDTO insertFileInfoDTO) {
        fileService.insertFileInfo(insertFileInfoDTO, AuthUserContext.get().getUserId());
        return ServerResponseEntity.success();
    }

    @PutMapping("/info")
    public ServerResponseEntity updateFileInfo(@RequestBody @Valid UpdateFileInfoDTO updateFileInfoDTO) {
        fileService.updateFileInfo(updateFileInfoDTO);
        return ServerResponseEntity.success();
    }

    @DeleteMapping("/info")
    public ServerResponseEntity deleteFileInfo(@NotNull(message = "fileId 不能为空~") Long fileId) {
        fileService.deleteFile(MinioConstant.FILE_PATH_PREFIX + MinioConstant.PATH_SEPARATOR + fileId, fileId);
        return ServerResponseEntity.success();
    }

    @GetMapping("/uploadUrl")
    public ServerResponseEntity<FileUrlVO> getUploadUrl() {
        return ServerResponseEntity.success(
                fileService.genUploadUrl(MinioConstant.FILE_PATH_PREFIX + MinioConstant.PATH_SEPARATOR)
        );
    }

    @GetMapping("/downloadUrl")
    public ServerResponseEntity<String> getDownloadUrl(@NotNull(message = "fileId 不能为空~") Long fileId) {
        return ServerResponseEntity.success(
                fileService.genDownloadUrl(MinioConstant.FILE_PATH_PREFIX + MinioConstant.PATH_SEPARATOR + fileId)
        );
    }
}
